﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using EntityFramework.Database.Management;
using NUnit.Framework;
using Rhino.Mocks;
using Sample.Application;

namespace Tests.Sample.Application
{
    public partial class StuffRepositoryTests
    {
        [TestFixture]
        public class WhenLoading
        {
            private IObjectContextProvider _contextProvider;
            private IObjectContext _context;
            private IQueryable<Stuff> _stuffs;
            private Stuff _stuff;
            private int _id;
            private StuffRepository _repository;

            [SetUp]
            public void SetUp()
            {
                _stuff = new Stuff() { ID = _id };
                _stuffs = new List<Stuff> { _stuff, new Stuff() { ID = 6 }, new Stuff() { ID = 7 } }.AsQueryable();
                _contextProvider = MockRepository.GenerateMock<IObjectContextProvider>();
                _context = MockRepository.GenerateMock<IObjectContext>();

                _repository = new StuffRepository(_contextProvider);
            }

            [TearDown]
            public void TearDown()
            {
                _contextProvider.VerifyAllExpectations();
            }

            [Test]
            public void GetsStuffFromContextWhereIdProviderMatches()
            {
                _contextProvider.Expect(c => c.GetContext<MyContext>()).Return(_context);
                _context.Expect(c => c.GetIQueryable<Stuff>()).Return(_stuffs);

                var actual = _repository.Load(_id);

                Assert.AreSame(_stuff, actual);
            }
        }
    }
}